package com.batchengine.util;

import static com.batchengine.util.LoggingFactory.createLogger;
import org.apache.log4j.Logger;

import java.lang.reflect.Method;

/**

 * User: sunil.incubator@yahoo.com
 * Date: Sep 20, 2009
 * Time: 1:38:47 AM
 */
public class ReflectionUtil {

    private static Logger logger = createLogger();

    public static Object createObjectzForClaszzName(String clazzName) {

        try {

            Class<?> aClass = Class.forName(clazzName);
            return aClass.newInstance();
        } catch (Exception e) {
            logger.error("Exception occured while creating objectz for the clazz name :: " + clazzName, e);
            throw new RuntimeException(e);
        } catch (Error e) {
            logger.error("Error occured while creating objectz for the clazz name :: " + clazzName, e);
            throw e;
        }
    }

    public static String getPropertyValue(Object object, String parameterName) {

        try {

            Method method = object.getClass().getMethod(new StringBuilder().append("get").
                    append(String.valueOf(parameterName.charAt(0)).toUpperCase()).
                    append(parameterName.substring(1)).toString());
            return method.invoke(object, new Object[]{null}).toString();
        } catch (Exception e) {
            logger.error("error in getting the table column name", e);
            return null;
        }
    }

    public static Object getPropertyValueObjectz(Object object, String parameterName) {

        try {

            Method method = object.getClass().getMethod(new StringBuilder().append("get").
                    append(String.valueOf(parameterName.charAt(0)).toUpperCase()).
                    append(parameterName.substring(1)).toString());
            return method.invoke(object, new Object[]{null}).toString();
        } catch (Exception e) {
            logger.error("error in getting the table column name", e);
            return null;
        }
    }

}
